package com.huarui.companypackage.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huarui.company.model.CompanyMarket;
import com.huarui.companypackage.dao.ICompanyMarketDao;
import com.huarui.companypackage.util.CompanyPackageConstant;
import com.huarui.market.dao.IMarketDao;
import com.huarui.market.model.Market;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
/**
 * 汽修厂购买套餐,companyMarket保存规则
 * @author Administrator
 *
 */
public class CompanyMarket4RepairStoreService implements CompanyMarketByCompanyTypeService{
	private IMarketDao marketDao;
	private ICompanyMarketDao companyMarketDao;

	@Override
	public Map<String, List<CompanyMarket>> getCompanyMarket4CompanyByType(List<String> marketIds, int duration,Integer mainMarketId) {
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer companyId = sessionMessage.getCompanyId();
		List<CompanyMarket> companyAddedMarkets = new ArrayList<CompanyMarket>();
		List<CompanyMarket> companyUpdateMarkets = new ArrayList<CompanyMarket>();
		List<CompanyMarket> searchableMarkets  = companyMarketDao.getSearchableCompanyMarketByCompanyId(companyId);					
		if (searchableMarkets.isEmpty()){
			//首次购买套餐
			for(String marketId : marketIds){
				Integer mId = Integer.valueOf(marketId);
				//搜索市场
				CompanyMarket searchMarket = new CompanyMarket();
				searchMarket.setCompanyId(companyId);
				searchMarket.setDuration(duration);
				searchMarket.setMarketId(mId);
				searchMarket.setSearchable(CompanyMarket.SEARCHABLE);
				searchMarket.setBefore(false);
				companyAddedMarkets.add(searchMarket);
			}
			//发布市场
			CompanyMarket publishMarket = new CompanyMarket();
			publishMarket.setCompanyId(companyId);
			publishMarket.setDuration(duration);
			publishMarket.setMarketId(mainMarketId);//主市场
			publishMarket.setPublishable(CompanyMarket.PUBLISHABLE);
			publishMarket.setBefore(false);
			companyAddedMarkets.add(publishMarket);
			//保险发布市场
			CompanyMarket insuranceMarket = new CompanyMarket();
			insuranceMarket.setCompanyId(companyId);
			insuranceMarket.setDuration(duration);				
			insuranceMarket.setMarketId(marketDao.getMarketByMarketType(Market.MARKET_TYPE_INSURE).get(0).getId());						
			insuranceMarket.setPublishable(CompanyMarket.PUBLISHABLE);
			insuranceMarket.setBefore(false);
			companyAddedMarkets.add(insuranceMarket);
		}else {
			Map<Integer,CompanyMarket> mapSearchableMarkets = new HashMap<Integer, CompanyMarket>();
			for(CompanyMarket searchableMarket : searchableMarkets){
				mapSearchableMarkets.put(searchableMarket.getMarketId(), searchableMarket);
			}						
			//当前购买时间(数据库时间)
			Date curDay = companyMarketDao.getCurDay();
			//当前使用套餐到期时间
			Date overDue = searchableMarkets.get(0).getGmtEnd();
			//判断下一个套餐购买时前一个套餐是否已到期
			int daysBetween = curDay.compareTo(overDue);
			//已购买的发布市场
			List<CompanyMarket> publishableMarkets = companyMarketDao.getPublishableCompanyMarketByCompanyId(companyId);
			for (String marketId : marketIds) {
				Integer mId = Integer.valueOf(marketId);
				CompanyMarket searchableMrket = mapSearchableMarkets.get(mId);
				if(searchableMrket != null){
					//汽修厂已购买过该搜索市场,更新有效期
					if (daysBetween <= 0) {
						//套餐未到期前购买套餐，更新该companyMarket的gmtEnd,gmtModify
						searchableMrket.setDuration(duration);
						searchableMrket.setGmtEnd(searchableMrket.getGmtEnd());
						searchableMrket.setBefore(true);
						companyUpdateMarkets.add(searchableMrket);
						for(CompanyMarket publishableMarket : publishableMarkets){
							publishableMarket.setDuration(duration);
							publishableMarket.setGmtEnd(publishableMarket.getGmtEnd());
							publishableMarket.setBefore(true);
							companyUpdateMarkets.add(publishableMarket);
						}
					} else if (daysBetween > 0) {
						//套餐已过期再购买套餐,更新gmtBegin,gmtEnd,gmtModify
						searchableMrket.setDuration(duration);
						searchableMrket.setBefore(false);
						companyUpdateMarkets.add(searchableMrket);
						for(CompanyMarket publishableMarket : publishableMarkets){
							publishableMarket.setDuration(duration);
							publishableMarket.setBefore(false);
							companyUpdateMarkets.add(publishableMarket);
						}
					}								
				}else{
					//汽修厂未购买该搜索市场，创建该市场
					CompanyMarket searchMarket = new CompanyMarket();
					if (daysBetween <= 0) {									
						searchMarket.setCompanyId(companyId);
						searchMarket.setDuration(duration);
						searchMarket.setMarketId(mId);
						searchMarket.setGmtBegin(overDue);
						searchMarket.setSearchable(CompanyMarket.SEARCHABLE);
						searchMarket.setBefore(true);
					}else if(daysBetween > 0){								
						searchMarket.setCompanyId(companyId);
						searchMarket.setDuration(duration);
						searchMarket.setMarketId(mId);
						searchMarket.setSearchable(CompanyMarket.SEARCHABLE);
						searchMarket.setBefore(false);
					}
					companyAddedMarkets.add(searchMarket);
				}				
			}
		}
		Map<String,List<CompanyMarket>> map = new HashMap<String, List<CompanyMarket>>();
		map.put(CompanyPackageConstant.COMPANYADDEDMARKETS.getType(), companyAddedMarkets);
		map.put(CompanyPackageConstant.COMPANYUPDATEMARKETS.getType(),companyUpdateMarkets);
		return map;
	}
	

	public void setMarketDao(IMarketDao marketDao) {
		this.marketDao = marketDao;
	}

	public void setCompanyMarketDao(ICompanyMarketDao companyMarketDao) {
		this.companyMarketDao = companyMarketDao;
	}
}
